package com.nbsaas.boot.monitor;

import com.lmax.disruptor.RingBuffer;
import org.springframework.stereotype.Component;

@Component
public class ApiMonitorDisruptor {

    private final RingBuffer<ApiLogEvent> ringBuffer;

    public ApiMonitorDisruptor(com.lmax.disruptor.dsl.Disruptor<ApiLogEvent> disruptor) {
        this.ringBuffer = disruptor.getRingBuffer();
    }

    public void publish(ApiLogEvent source) {
        long sequence = ringBuffer.next();
        try {
            ApiLogEvent event = ringBuffer.get(sequence);
            event.setTenantId(source.getTenantId());
            event.setUri(source.getUri());
            event.setMethod(source.getMethod());
            event.setStatus(source.getStatus());
            event.setStartTime(source.getStartTime());
            event.setDuration(source.getDuration());
            event.setSuccess(source.isSuccess());
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}